<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>基本资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
    <style>
        .layui-form-item .layui-input-company {
            width: auto;
            padding-right: 10px;
            line-height: 38px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" id="updateUser">
            <div class="layui-form-item">
                <div class="layui-hide">
                    <input type="text" id="id" name="id"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" id="username" name="username" lay-verify="required" lay-reqtext="管理账号不能为空"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">密码</label>
                <div class="layui-input-inline">
                    <input type="text" id="password" name="password" lay-verify="required"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" id="image">
                <label class="layui-form-label">头像</label>
                <div class="layui-input-inline">
                    <img style="object-fit: scale-down;width: 100px;height: 100px;border-radius: 30px">
                </div>
            </div>
            <div class="layui-form-item">
                <input type="hidden" id="imagePath" name="urls" value="">
                <label class="layui-form-label">上传头像</label>
                <div class="layui-upload-drag" id="test10">
                    <i class="layui-icon"></i>
                    <p>点击上传新头像</p>
                    <div class="layui-hide" id="uploadDemoView">
                        <hr>
                        <img src="" alt="上传成功后渲染" style="max-width: 196px">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">手机号</label>
                <div class="layui-input-inline">
                    <input type="number" id="phone" name="phone" lay-verify="required" maxlength="11"
                           lay-reqtext="手机号不能为空" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
<script>
    var jsonAdmin = localStorage.getItem("admin");//获取数据
    var admin = JSON.parse(jsonAdmin);//类型转换
    layui.$("#username").attr('value', admin.username)//显示数据
    layui.$("#password").attr('value', admin.password)//显示数据
    layui.$("#phone").attr('value', admin.phone)//显示数据
    layui.$("#id").attr('value', admin.username)//显示数据
    layui.$('#image').find('img').attr('src', admin.urls);
</script>
<script>
    layui.use(['form', 'miniTab'], function () {
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload
        miniTab = layui.miniTab;

        upload.render({
            elem: '#test10' //绑定上传图片button的id
            , url: 'admin/upload image' //上传接口
            , done: function (res) {
                //上传完毕回调

                if (res.code == 1) {
                    layer.msg("上传成功");
                    let src = res.src;//把图片路径返回过来
                    layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', src);
                    console.log(res)
                    $("#imagePath").attr("value", src);//给图片路径隐藏域赋值
                }
            }
            , error: function () {
                //请求异常回调
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data = data.field;
            var img = $("#imagePath").val()
            if (img == "") {
                layui.$("#imagePath").attr("value", admin.urls)
            }
            $.get("admin/edit", $("#updateUser").serialize(),
                function (data) {
                alert("更新成功")
                            window.location = 'login.html';
                });

        });
    });
</script>
</body>
</html>